const common = {
  namespaced: true,
  state: {
    camp: {
      type: [],
      list: [],
      info: {},
    }
  },
  mutations: {
    //微信支付
    wxPay(state, payload) {
      function onBridgeReady() {
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest', {
            "appId": payload.appId, //公众号名称，由商户传入     
            "timeStamp": payload.timeStamp, //时间戳，自1970年以来的秒数     
            "nonceStr": payload.nonceStr, //随机串     
            "package": payload.package,
            "signType": "MD5", //微信签名方式：     
            "paySign": payload.paySign //微信签名 
          },
          function (res) {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
              // 使用以上方式判断前端返回,微信团队郑重提示：
              //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
              global.router.replace({
                name: "myCamp",
                query: {
                  state: 'wxPay'
                },
              });
              Toast.success('支付成功！');
            }
          }
        );
      }
      if (typeof WeixinJSBridge == "undefined") {
        if (document.addEventListener) {
          document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        } else if (document.attachEvent) {
          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
      } else {
        onBridgeReady();
      }
    },
  },
  actions: {
    GetCampTypeList(context, param) {
      // 获取营地类型列表
      global.POST(global.api.GetCampTypeList, {
        Page: 1,
        Rows: 999,
      }).then(res => {
        context.state.camp.type = res._CampTypeList
      })
    },
    GetCampList(context, param) {
      // 获取营地列表
      global.POST(global.api.GetCampList, {
        CampTypeID: param.type,
        Page: param.deploy.Page,
        Rows: param.deploy.Rows
      }).then(res => {
        if (param.deploy.Page == 1)
          context.state.camp.list = res._CampList
        else
          context.state.camp.list = context.state.camp.list.concat(res._CampList)
        if (param.ref != undefined) {
          if (res._CampList.length < param.deploy.Rows) {
            param.ref.finishInfinite(true)
          } else {
            param.ref.finishPullToRefresh()
            if (param.pageAdd != undefined)
              param.pageAdd()
          }
        }
        if (param.success != undefined) {
          param.success()
        }
      })
    },
    GetCampInfo(context, param) {
      // 获取营地详情
      global.POST(global.api.GetCampInfo, {
        CampID: param,
      }).then(res => {
        context.state.camp.info = res._Camp
      })
    },
    CampCommit({
      commit
    }, param) {
      // 营地报名
      global.POST(global.api.CampCommit, {
        CampID: param
      }).then(res => {
        if(global.$store.state.camp.camp.info.Camp_Type == 1){
          global.router.replace({
            name: "myCamp",
            query: {
              state: 'wxPay'
            },
          });
          Toast.success('报名成功！');
          return
        }
        commit('wxPay', res)
      })
    },
  },
  getters: {}
}
export default common